package com.woniu.config;

import com.woniu.inteceptor.LoginInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 跨域配置
 */
@Configuration
public class MyMvcConfig implements WebMvcConfigurer {
    //跨域设置
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                //可以跨域的域名
                .allowedOriginPatterns("*")
                //是否支持发送cookie
                .allowCredentials(true)
                //放行的请求方式
                .allowedMethods("GET", "POST", "DELETE", "PUT", "OPTIONS")
                //放行的原始头信息
                .allowedHeaders("*")
                //暴露的头信息
                .exposedHeaders("*")
                .maxAge(3600);
    }

    //注入登录拦截器
    @Bean
    public LoginInterceptor loginInterceptor() {
        return new LoginInterceptor();
    }

    //登录拦截器放行的路径
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] path = {
                "/api/login",
                "/getCode",
                "/api/register",
                "/error",
                "/webjars/**",
                "/swagger-resources",
                "/swagger-resources/**",
                "/favicon.ico",
                "/alipay/**",
        };
        //指定登录拦截目标路径
        registry.addInterceptor(this.loginInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(path);
    }
}
